package com.lut.web;

import com.lut.pojo.Cart;
import com.lut.pojo.User;
import com.lut.service.OrderService;
import com.lut.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @user:dell
 * @date:2020年10月17 15:22
 */
public class OrderServlet extends  BaseServlet{
    private OrderService orderService=new OrderServiceImpl();
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//     获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
//        获取用户id
        User loginUser = (User) req.getSession().getAttribute("user");
        if(loginUser==null){
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
            return;
        }
        Integer userId = loginUser.getId();
        //        调用createOrder方法
        String orderId= orderService.createOrder(cart, userId);


        //        保存对象到session域中
//        req.setAttribute("orderId",orderId);
        req.getSession().setAttribute("orderId",orderId);
//        请求转发
//        req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req,resp);
        resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");

    }
}
